package tt.dz.biz.dao.paymentgateway;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.PayGateVO;

@Repository("paymentWateWayDAO")
public class PaymentWateWayDAOImp
  extends BaseDAO
{
  public static final String QUERY_PAY_CHANNEL_FORSEL = "select  t.channel_code,t.channel_name from MS_PAY_channel as t where t.is_valid=1 ";
  public static final String QUERY_MERCHANT_FORSEL = "select  t.merchant_code,t.name from MS_MERCHANT as t where t.is_valid=1 ";
  public static final String QUERY_PAY_GATE_LIST = "select  t.merchant_code,t.channel_code,t.account_id,t.public_key,t.sign_type,t.remark,t.is_valid  from ms_pay_gate as t  ";
  public static final String INSERT_PAY_GATE = "insert into ms_pay_gate(merchant_code,channel_code,account_id,public_key,sign_type,remark,is_valid)  values(:merchantCode,:channelCode,:accountId,:publicKey,:signType,:remark,:isValid )";
  public static final String UPDATE_PAY_GATE = "update ms_pay_gate  set account_id=:accountId,public_key=:publicKey,sign_type=:signType,remark=:remark,is_valid=:isValid  where  merchant_code=:merchantCode and channel_code=:channelCode ";
  public static final String CHECK_PAY_GATE_EXISTS = "select 1 from ms_pay_gate as t  where merchant_code=:merchantCode and channel_code=:channelCode ";
  public static final String QUERY_PAY_GATE_PRIMARYKEY = "select  t.merchant_code,t.channel_code,t.account_id,t.public_key,t.sign_type,t.remark,t.is_valid  from ms_pay_gate as t  where t.is_valid=1  t.merchant_code=:merchantCode and t.channel_code=:channelCode ";
  
  public List<Map<String, Object>> getPayChannelSel()
    throws Exception
  {
    LogUtil.debug("查询所有渠道下拉值");
    MapSqlParameterSource paramMap = null;
    List<Map<String, Object>> payChannelSelList = queryForList("select  t.channel_code,t.channel_name from MS_PAY_channel as t where t.is_valid=1 ", paramMap);
    return payChannelSelList;
  }
  
  public List<Map<String, Object>> getMerchantSel()
    throws Exception
  {
    LogUtil.debug("查询所有运营商下拉值");
    MapSqlParameterSource paramMap = null;
    List<Map<String, Object>> payChannelSelList = queryForList("select  t.merchant_code,t.name from MS_MERCHANT as t where t.is_valid=1 ", paramMap);
    return payChannelSelList;
  }
  
  public List<PayGateVO> getPayGateList(Map<String, ?> paramMap)
    throws Exception
  {
    LogUtil.debug("查询支付网关列表信息");
    MapSqlParameterSource parameters = new MapSqlParameterSource(paramMap);
    List<PayGateVO> payGateList = queryForList("select  t.merchant_code,t.channel_code,t.account_id,t.public_key,t.sign_type,t.remark,t.is_valid  from ms_pay_gate as t  ", parameters, PayGateVO.class);
    return payGateList;
  }
  
  public PayGateVO getPayGateByID(PayGateVO payGateVO)
    throws Exception
  {
    LogUtil.debug("通过运营商编号与渠道编号查询支付网关信息");
    if (checkPayGateExists(payGateVO) == 1)
    {
      SqlParameterSource paramSource = new BeanPropertySqlParameterSource(payGateVO);
      PayGateVO payGate = (PayGateVO)getJdbcTemplate().queryForObject("select  t.merchant_code,t.channel_code,t.account_id,t.public_key,t.sign_type,t.remark,t.is_valid  from ms_pay_gate as t  where t.is_valid=1  t.merchant_code=:merchantCode and t.channel_code=:channelCode ", paramSource, PayGateVO.class);
      
      return payGate;
    }
    return null;
  }
  
  public int insertPayGate(PayGateVO payGateVO)
    throws Exception
  {
    LogUtil.debug("插入支付网关相关数据");
    return save("insert into ms_pay_gate(merchant_code,channel_code,account_id,public_key,sign_type,remark,is_valid)  values(:merchantCode,:channelCode,:accountId,:publicKey,:signType,:remark,:isValid )", payGateVO);
  }
  
  public int updatePayGate(PayGateVO payGateVO)
    throws Exception
  {
    LogUtil.debug("修改支付网关相关数据");
    return save("update ms_pay_gate  set account_id=:accountId,public_key=:publicKey,sign_type=:signType,remark=:remark,is_valid=:isValid  where  merchant_code=:merchantCode and channel_code=:channelCode ", payGateVO);
  }
  
  public int checkPayGateExists(PayGateVO payGateVO)
    throws Exception
  {
    LogUtil.debug("检查支付网关相关数据是否存在");
    Map<String, String> paramMap = new HashMap();
    paramMap.put("merchantCode", payGateVO.getMerchantCode());
    paramMap.put("channelCode", payGateVO.getChannelCode());
    LogUtil.debug("参数,merchantCode:" + payGateVO.getMerchantCode() + ";channelCode:" + payGateVO.getChannelCode());
    List<Integer> result = getJdbcTemplate().query("select 1 from ms_pay_gate as t  where merchant_code=:merchantCode and channel_code=:channelCode ", paramMap, new SingleColumnRowMapper());
    if (result.size() > 0) {
      return 1;
    }
    return 0;
  }
}
